var dbName = "cfentDB";
var dbPath = "fs://res/cfentDB.sqlite";
var table_name = "chat_msg_record";

function initDB() {
    var db = api.require('db');
    db.openDatabase({
        name: dbName,
        path: dbPath
    }, function (ret, err) {
        if (ret.status) {
            var sql = "CREATE TABLE IF NOT EXISTS " + table_name + " (conversationId text,myRingId text,messageId text);";
            db.executeSql({
                name: dbName,
                sql: sql
            }, function (ret, err) {
                console.log(JSON.stringify(ret));
            });
        }
    });
}

/**
 * 查找消息是否已读
 * @param conversationId 圈子id
 * @param myRingId 我的环信id
 * @param messageId 消息id
 */
function msgIsRead(conversationId, myRingId, messageId) {
    var db = api.require('db');
    var sql = "select * from " + table_name + " where conversationId=" + conversationId + " and myRingId=" + myRingId + " and messageId=" + messageId;
    // var sql = "SELECT * FROM "+table_name;
    var ret = db.selectSqlSync({
        name: dbName,
        sql: sql
    });
    return !(ret.data.length===0);
}

/**
 * 标记消息已读
 * @param conversationId 圈子id
 * @param myRingId 我的环信id
 * @param messageId 消息id
 */
function markRead(conversationId, myRingId, messageId) {
    var db = api.require('db');
    var sql = "insert into " + table_name + " values(" + conversationId + "," + myRingId + "," + messageId + ")";
    var ret = db.executeSqlSync({
        name: dbName,
        sql: sql
    });
    console.log(JSON.stringify(ret));
}